
# 1. 写文件
f1 = open("C:\\废弃文件库\\test.txt","w")

f1.write("这是一个普通的记事本\n")

f1.close()


# 2. 读文件
f2 = open("C:\\废弃文件库\\test.txt","r")

print(f2.read())

f2.close()



# 1. 关闭文件的上限
# 2. 一定要注意关闭文件，否则是很有可能出现内存泄漏的问题
# count = 0
# list1 = list()
# while True:
#     print(f"文件个数：{count}")
#     f = open("C:\\废弃文件库\\test.txt", "r")
#     list1.append(f)
#     count += 1
#     f.close()



# 3. 文件默认关闭操作
# with 获取对象 as 句柄
# list1 = list()
# while True:
#     with open("C:\\废弃文件库\\test.txt", "r") as f3:
#         print(f3.read())
#         list1.append(f3)


# 4. 文件内容追加
with open("C:\\废弃文件库\\test.txt","a") as f4:
    f4.write("春眠不觉晓\n")
    f4.write("处处闻啼鸟\n")
    f4.write("夜来风雨声\n")
    f4.write("花落知多少\n")